package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"time"
	"xorm.io/core"
)

type User struct {
	Id       int       `xorm:"not null pk autoincr INT(11)"`
	Username string    `xorm:"not null VARCHAR(32)"`
	Birthday time.Time `xorm:"DATE"`
	Sex      string    `xorm:"CHAR(1)"`
	Address  string    `xorm:"VARCHAR(256)"`
}

func main() {
	engine, err := xorm.NewEngine("mysql", "root:root@(127.0.0.1:3306)/test1?charset=utf8")

	if err != nil {
		fmt.Println(err)
		return
	}
	if err := engine.Ping(); err != nil {
		fmt.Println(err)
		return
	}
	// 设置连接池的空闲数大小
	engine.SetMaxIdleConns(5)
	// 设置最大打开连接数
	engine.SetMaxOpenConns(5)
	// 名称映射规则主要负责结构体名称到表名和结构体field到表字段的名称映射
	engine.SetColumnMapper(core.SameMapper{})
	// 则会在控制台打印出生成的SQL语句
	engine.ShowSQL(true)
	// 则会在控制台打印调试及以上的信息
	engine.Logger().SetLevel(core.LOG_DEBUG)

	// 增加数据
	user := new(User)
	user.Username = "tyming"
	affected, err := engine.Insert(user)
	fmt.Println(affected, err)

	//删除数据
	user1 := new(User)
	user1.Username = "tyming"
	affected_delete, err := engine.Delete(user1)
	fmt.Println(affected_delete)

	//修改数据
	user2 := new(User)
	user2.Username = "tyming"
	affected_update, err := engine.Id(1).Update(user2)
	fmt.Println(affected_update)

	// 查询
	user3 := new(User)
	result, err := engine.Id(1).Get(user3)
	//retult, err := engine.Where("id=?", 1).Get(user3)
	fmt.Println(result)
}

//从视图批量获取用户组分页信息
//func  BatchGetUserArrPageInfoFormView(uid []uint32) ([]*models.VUserInfo, error) {
//	en, ok := this.Agt.Get(MYDB_CUSTOMER)
//	if !ok {
//		return nil, errors.New("获取数据库错误!")
//	}
//
//	users := make([]*models.VUserInfo, 0)
//	err := en.Where(builder.In("user_id", uid)).Find(&users)
//
//	if err != nil {
//		return nil, err
//	}
//	return users, nil
//}
